# -*- coding: utf-8 -*-
'''
Created on 2013-4-23

@author: dyq
'''
from Common.htmlHlep import Find
from Common.tool import tool
from Cms.plus import  page,user
from Models import  models
from django.db.models import Max

@page.page_render(user.Admin())
def Index(request):
        '''
                            列表页
        '''
        list= models.Sys_Link.objects.filter().order_by("Sno") #倒序加上-号
    
        return Find.View(request,list)

    
@page.page_render(user.Admin())
def Add(request):
        '''
                           添加
        '''
        return Find.View(request)
    
@page.page_render(user.Admin())
def DoAdd(request):
        '''
                            添加
        '''
        sno = models.Sys_Link.objects.filter().aggregate(Max('Sno'))["Sno__max"]
        print sno==None 
        linkId=tool.guid()
        info = models.Sys_Link(LinkId=linkId)
        Find.UpdateModel(request,info)
        if sno==None:
            info.Sno=0
        else:
            info.Sno=sno+1
        info.save()
        return Find.JsonX(False, "操作成功")
    

@page.page_render(user.Admin())
def Edit(request):
        '''
                            编辑
        '''
        info =models.Sys_Link.objects.get(LinkId=request.GET.get("LinkId"))
        
        return Find.View(request,info)
    
@page.page_render(user.Admin())
def DoEdit(request):
        '''
                            编辑
        '''
        info=models.Sys_Link.objects.get(LinkId=request.POST.get("LinkId"))
        Find.UpdateModel(request,info)
        info.save()
        return Find.JsonX(False, "操作成功")
    
@page.page_render(user.Admin())
def DoDel(request):
        '''
                            删除
        '''
        models.Sys_Link.objects.get(LinkId=request.GET.get("LinkId")).delete()
        return Find.JsonX(False, "操作成功")
        
@page.page_render(user.Admin())
def DoMobile(request):
        '''
                            移动到
        '''
        
        dataOrderList=str(request.POST.get("dataOrderid")).split(',')
        i=1
        for key in dataOrderList:
            info=models.Sys_Link.objects.get(LinkId=key)
            if info.Sno!=i:
                info.Sno=i
                info.save()
            i=i+1
        
        return Find.JsonX(False, "操作成功")

    
    